﻿using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.ClassFactory;
using Common;
using DataAccess;
using Util.PageInfo;
using ValidateCommon;
using System.Globalization;
using System.Data;
using System.Xml;

namespace RichECMS.ServerPages.Section
{
    public partial class List : Util.BasePage.BasePage
    {
        public int intSectionID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request["Section"] != null)
            {
                if (ParamAuth.IsNumber(Request["Section"].ToString()))
                {
                    State.intPageIndex = 1;
                    State.intSTID = int.Parse(Request["Section"].ToString());
                    BusinessLogic.BusinessRules.Sections.FindSectionReferRoot(State.intSTID, ref intSectionID);
                    if (!IsPostBack)
                    {
                        State.strWhereSql = "";
                        this.btnAdd.HRef = this.btnAdd.HRef + "?Section=" + Request["Section"].ToString();
                        this.btnSection.HRef = this.btnSection.HRef + "?Section=" + Request["Section"].ToString();
                        //InitDataBind(State.intPageIndex, AspNetPagerList.PageSize);
                        InitDataBind(State.intPageIndex, int.Parse(ddlShowCount.SelectedValue), ddlShowSort.SelectedValue);
                        AspNetPagerList.CurrentPageIndex = State.intPageIndex;
                        LitPaagePath.Text = PageInfo.ShowPagePath(State.intSTID);
                        DropDownListMainSectionlist.SelectedIndex = 0;
                    }
                }
                else
                {
                    Response.Write("参数错误!");
                    Response.End();
                    return;
                }
            }
            else
            {
                Response.Write("参数错误!");
                Response.End();
                return;
            }
        }

        /// <summary>
        /// 设置要查找文章的栏目
        /// </summary>
        /// <param name="strSecArray"></param>
        /// <param name="bolEnableSearchChildSection"></param>
        private void SearchSection(ref string strSecArray, bool bolEnableSearchChildSection)
        {
            if (bolEnableSearchChildSection == false)
            {
                strSecArray = "(" + intSectionID + ",-1)";
            }
            else
            {
                string strSCList = intSectionID.ToString();
                GetSectionFromParentId(ref strSCList, intSectionID);
                strSecArray = "(" + strSCList + ",-1)";
            }
        }

        /// <summary>
        /// 更新标签
        /// </summary>
        /// <param name="strXML"></param>
        private void SyncDataLabel(string strXML)
        {
            try
            {
                if (strXML.Trim() == "")
                {
                    return;
                }

                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(strXML);
                //查找标签
                XmlNode nodeLabel = xmldoc.SelectSingleNode("//infotitle");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        Littitle.Text = nodeLabel.InnerXml;
                    }
                }
                //nodeLabel = xmldoc.SelectSingleNode("//infoclass");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LitInfoClass.Text = nodeLabel.InnerXml;
                //    }
                //}
                //nodeLabel = xmldoc.SelectSingleNode("//infosubtitle");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LitSubTitle.Text = nodeLabel.InnerXml;
                //    }
                //}
                nodeLabel = xmldoc.SelectSingleNode("//infoArticleMan");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LitInfoArticleMan.Text = nodeLabel.InnerXml;
                    }
                }
                //nodeLabel = xmldoc.SelectSingleNode("//infoimage");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LitInfoImage.Text = nodeLabel.InnerXml;
                //    }
                //}
                //nodeLabel = xmldoc.SelectSingleNode("//infodescription");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LitInfoDescription.Text = nodeLabel.InnerXml;
                //    }
                //}
                //nodeLabel = xmldoc.SelectSingleNode("//infosummary");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LitInfoSummary.Text = nodeLabel.InnerXml;
                //    }
                //}
                //nodeLabel = xmldoc.SelectSingleNode("//infocontent");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LitInfoContent.Text = nodeLabel.InnerXml;
                //    }
                //}
                //nodeLabel = xmldoc.SelectSingleNode("//infosource");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LiteralSource.Text = nodeLabel.InnerXml;
                //    }
                //}
                //nodeLabel = xmldoc.SelectSingleNode("//infoauthor");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LiteralAuthor.Text = nodeLabel.InnerXml;
                //    }
                //}
                //nodeLabel = xmldoc.SelectSingleNode("//infoalltop");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LiteralAllTop.Text = nodeLabel.InnerXml;
                //    }
                //}
                //nodeLabel = xmldoc.SelectSingleNode("//infosectiontop");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LiteralSectonTop.Text = nodeLabel.InnerXml;
                //    }
                //}
                //nodeLabel = xmldoc.SelectSingleNode("//infoalltj");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LiteralAllTJ.Text = nodeLabel.InnerXml;
                //    }
                //}
                //nodeLabel = xmldoc.SelectSingleNode("//infosectiontj");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LiteralSectionTJ.Text = nodeLabel.InnerXml;
                //    }
                //}
                //nodeLabel = xmldoc.SelectSingleNode("//infoscrollnews");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LiteralScrollNews.Text = nodeLabel.InnerXml;
                //    }
                //}
                //nodeLabel = xmldoc.SelectSingleNode("//infoverify");
                //if (nodeLabel != null)
                //{
                //    if (nodeLabel.InnerXml.Trim() != "")
                //    {
                //        LiteralVerify.Text = nodeLabel.InnerXml;
                //    }
                //}
            }
            catch (Exception ex)
            {
                Alert(ex.Message.Replace("\r\n", ""));
            }
        }

        /// <summary>
        /// 根据父ID获取子栏目
        /// </summary>
        private static void GetSectionFromParentId(ref string strSTIDlist, int intParentId)
        {
            try
            {
                using (SectionsFactory objFactory = new SectionsFactory())
                {
                    BusinessLogic.BusinessRules.Sections objsection = objFactory.CreatorBySql("intParentID =" + intParentId.ToString());
                    if (objsection != null)
                    {
                        BusinessLogic.BusinessRules.Sections objsectionTemp = objFactory.NewInstance();
                        for (int i = 0; i < objsection.Count; i++)
                        {
                            strSTIDlist = strSTIDlist + "," + objsection.DataItem.intSectionID;
                            objsectionTemp = objFactory.CreatorBySql("intParentID =" + objsection.DataItem.intSectionID.ToString());
                            if (objsectionTemp != null)
                            {
                                for (int j = 0; j < objsectionTemp.Count; j++)
                                {
                                    strSTIDlist = strSTIDlist + "," + objsectionTemp.DataItem.intSectionID;
                                    GetSectionFromParentId(ref strSTIDlist, objsectionTemp.DataItem.intSectionID);
                                    objsectionTemp.SelectRow(j + 1);
                                }
                            }
                            objsection.SelectRow(i + 1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("获取子栏目ID是发生异常：" + ex.Message.Replace("\r\n", ""));
            }
        }

        /// <summary>
        /// 绑定
        /// </summary>
        private void InitDataBind(int intCurrentPage, int intPageSize, string strSort)
        {
            //设置分页条数
            AspNetPagerList.PageSize = intPageSize;
            try
            {
                //绑定文章类型
                DropDownListMainSectionlist.Items.Clear();
                ListItem listitem = new ListItem("--请选择板块--", "-1");
                DropDownListMainSectionlist.Items.Add(listitem);
                using (SectionsFactory objFactory = new SectionsFactory())
                {
                    BusinessLogic.BusinessRules.Sections objsection = objFactory.CreatorBySql("intParentID =0 and intWSID=" + State.intWSID + " order by intSortNo desc,  dtmInTime desc");
                    if (objsection != null)
                    {
                        for (int i = 0; i < objsection.Count; i++)
                        {
                            if (objsection.DataItem.intType == (int)Utility.STType.List)
                            {
                                listitem = new ListItem(objsection.DataItem.chvSTCName + (objsection.DataItem.IstxtRemarksNull() ? "" : "(" + objsection.DataItem.txtRemarks + ")"),
                                                        objsection.DataItem.intSectionID.ToString());
                                DropDownListMainSectionlist.Items.Add(listitem);
                            }
                            objsection.SelectRow(i + 1);
                        }
                    }
                }
                using (ArticleFactory objFactory = new ArticleFactory())
                {
                    //构建栏目序列
                    string strSectionArray = "";
                    SearchSection(ref strSectionArray, CheckBoxSearch.Checked);

                    StringBuilder strWhere = new StringBuilder();
                    using (ArtSecRelationFactory objArtSecRelationFactory = new ArtSecRelationFactory())
                    {
                        BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objArtSecRelationFactory.NewInstance();

                        int intTotalCounts = 0;

                        using (SectionsFactory objSectionFactory = new SectionsFactory())
                        {
                            BusinessLogic.BusinessRules.Sections objSections = objSectionFactory.Creator(State.intSTID);
                            if (objSections != null)
                            {
                                DataTable tblTmp = objSections.Data.Tables[0];
                                DataRow row = tblTmp.Rows[0];
                                if (row["chvDataLabel"] != DBNull.Value)
                                {
                                    SyncDataLabel(objSections.DataItem.chvDataLabel);
                                }

                                if (intPageSize == 0)
                                {
                                    intPageSize = objArtSecRelation.GetTotalCount("intSectionID in " + strSectionArray + " and intWSID=" + State.intWSID);  //显示所有
                                }
                                int intPageFrom = intCurrentPage * intPageSize;

                                if (State.strWhereSql.Trim().Length == 0)
                                {
                                    intTotalCounts = objArtSecRelation.GetTotalCount("intSectionID in " + strSectionArray + " and intWSID=" + State.intWSID);
                                }
                                else
                                {
                                    intTotalCounts = objSections.GetTotalCountByCustomSql("SELECT count(*) FROM (select Top " + (intPageFrom).ToString() + " Article.chvTitle,Article.chvArticleInMan,Article.dtmInTime,Article.chvSummary,Article.chvMainMiniPic,Article.chvMainPic, ArtSecRelation.* from Article,ArtSecRelation" + " where Article.intArticleID = ArtSecRelation.intArticleID and ArtSecRelation.intWSID = " + State.intWSID + " and ArtSecRelation.intSectionID in " + strSectionArray + State.strWhereSql + " order by ArtSecRelation.intSort desc ,Article.dtmInTime desc,ArtSecRelation.intSecArtID desc)t ");
                                }

                                //显示版块名称
                                LitPageName.Text = objSections.DataItem.chvSTCName;
                                LitPageCurrent.Text = intCurrentPage.ToString();
                                LitPageCount.Text = intTotalCounts.ToString();
                                if (intTotalCounts % intPageSize != 0)
                                {
                                    if ((intCurrentPage == intTotalCounts / intPageSize + 1))
                                    {
                                        intPageFrom = (intCurrentPage - 1) * intPageSize;
                                        //intPageSize =Model.Config.JudgeDatabase( intTotalCounts % intPageSize);//处理不同数据库差异
                                        intPageSize = intTotalCounts % intPageSize;
                                        intPageFrom = intPageFrom + intPageSize;
                                    }
                                }

                                if (strSort == null || strSort == "" || strSort.Trim().ToLower() == "default")
                                {
                                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.chvTitle,Article.chvArticleInMan,Article.dtmInTime,Article.chvSummary,Article.chvMainMiniPic,Article.chvMainPic,ArtSecRelation.* from Article,ArtSecRelation " + " where Article.intArticleID = ArtSecRelation.intArticleID and ArtSecRelation.intWSID=" + State.intWSID + " and ArtSecRelation.intSectionID in " + strSectionArray + " " + State.strWhereSql + " order by ArtSecRelation.intSort desc ,Article.dtmInTime desc,ArtSecRelation.intSecArtID desc)t order by intSort asc, dtmInTime asc ,intSecArtID asc)m " + " order by intSort asc, dtmInTime asc ,intSecArtID asc)mm  order by intSort desc, dtmInTime desc ,intSecArtID desc");
                                }
                                else if (strSort.Trim().ToLower() == "sortdesc")
                                {
                                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.chvTitle,Article.chvArticleInMan,Article.dtmInTime,Article.chvSummary,Article.chvMainMiniPic,Article.chvMainPic,ArtSecRelation.* from Article,ArtSecRelation " + " where Article.intArticleID = ArtSecRelation.intArticleID and ArtSecRelation.intWSID=" + State.intWSID + " and ArtSecRelation.intSectionID in " + strSectionArray + " " + State.strWhereSql + " order by ArtSecRelation.intSort desc )t order by intSort asc)m " + " order by intSort asc)mm  order by intSort desc");
                                }
                                else if (strSort.Trim().ToLower() == "sortasc")
                                {
                                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.chvTitle,Article.chvArticleInMan,Article.dtmInTime,Article.chvSummary,Article.chvMainMiniPic,Article.chvMainPic,ArtSecRelation.* from Article,ArtSecRelation " + " where Article.intArticleID = ArtSecRelation.intArticleID and ArtSecRelation.intWSID=" + State.intWSID + " and ArtSecRelation.intSectionID in " + strSectionArray + " " + State.strWhereSql + " order by ArtSecRelation.intSort asc )t order by intSort desc)m " + " order by intSort desc)mm  order by intSort asc");
                                }
                                else if (strSort.Trim().ToLower() == "timedesc")
                                {
                                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.chvTitle,Article.chvArticleInMan,Article.dtmInTime,Article.chvSummary,Article.chvMainMiniPic,Article.chvMainPic,ArtSecRelation.* from Article,ArtSecRelation " + " where Article.intArticleID = ArtSecRelation.intArticleID and ArtSecRelation.intWSID=" + State.intWSID + " and ArtSecRelation.intSectionID in " + strSectionArray + " " + State.strWhereSql + " order by Article.dtmInTime desc)t order by dtmInTime asc)m " + " order by  dtmInTime asc )mm  order by dtmInTime desc");
                                }
                                else if (strSort.Trim().ToLower() == "timeasc")
                                {
                                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.chvTitle,Article.chvArticleInMan,Article.dtmInTime,Article.chvSummary,Article.chvMainMiniPic,Article.chvMainPic,ArtSecRelation.* from Article,ArtSecRelation " + " where Article.intArticleID = ArtSecRelation.intArticleID and ArtSecRelation.intWSID=" + State.intWSID + " and ArtSecRelation.intSectionID in " + strSectionArray + " " + State.strWhereSql + " order by Article.dtmInTime asc)t order by dtmInTime desc)m " + " order by  dtmInTime desc )mm  order by dtmInTime asc");
                                }
                                else if (strSort.Trim().ToLower() == "iddesc")
                                {
                                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.chvTitle,Article.chvArticleInMan,Article.dtmInTime,Article.chvSummary,Article.chvMainMiniPic,Article.chvMainPic,ArtSecRelation.* from Article,ArtSecRelation " + " where Article.intArticleID = ArtSecRelation.intArticleID and ArtSecRelation.intWSID=" + State.intWSID + " and ArtSecRelation.intSectionID in " + strSectionArray + " " + State.strWhereSql + " order by ArtSecRelation.intSecArtID desc)t order by intSecArtID asc)m " + " order by intSecArtID asc)mm  order by intSecArtID desc");
                                }
                                else if (strSort.Trim().ToLower() == "idasc")
                                {
                                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.chvTitle,Article.chvArticleInMan,Article.dtmInTime,Article.chvSummary,Article.chvMainMiniPic,Article.chvMainPic,ArtSecRelation.* from Article,ArtSecRelation " + " where Article.intArticleID = ArtSecRelation.intArticleID and ArtSecRelation.intWSID=" + State.intWSID + " and ArtSecRelation.intSectionID in " + strSectionArray + " " + State.strWhereSql + " order by ArtSecRelation.intSecArtID asc)t order by intSecArtID desc)m " + " order by intSecArtID desc)mm  order by intSecArtID asc");
                                }
                                else
                                {
                                    strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.chvTitle,Article.chvArticleInMan,Article.dtmInTime,Article.chvSummary,Article.chvMainMiniPic,Article.chvMainPic,ArtSecRelation.* from Article,ArtSecRelation " + " where Article.intArticleID = ArtSecRelation.intArticleID and ArtSecRelation.intWSID=" + State.intWSID + " and ArtSecRelation.intSectionID in " + strSectionArray + " " + State.strWhereSql + " order by ArtSecRelation.intSort desc ,Article.dtmInTime desc,ArtSecRelation.intSecArtID desc)t order by intSort asc, dtmInTime asc ,intSecArtID asc)m " + " order by intSort asc, dtmInTime asc ,intSecArtID asc)mm  order by intSort desc, dtmInTime desc ,intSecArtID desc");
                                }

                                BusinessLogic.BusinessRules.ArtSecRelation ArtSecRelation = objArtSecRelationFactory.CreatorByFullSql(strWhere.ToString());
                                if (ArtSecRelation != null)
                                {
                                    rptList.DataSource = ArtSecRelation.Data.ArtSecRelation.DataSet.Tables[0].DefaultView;
                                    AspNetPagerList.RecordCount = intTotalCounts;
                                    rptList.DataBind();
                                    LitPageTotal.Text = AspNetPagerList.PageCount > 0 ? AspNetPagerList.PageCount.ToString() : "1";
                                }
                                else
                                {
                                    rptList.DataSource = objArtSecRelationFactory.NewInstance().Data.ArtSecRelation.DataSet.Tables[0].DefaultView;
                                    AspNetPagerList.RecordCount = 0;
                                    rptList.DataBind();
                                    LitPageTotal.Text = AspNetPagerList.PageCount > 0 ? AspNetPagerList.PageCount.ToString() : "1";
                                    //Alert("暂无数据！");
                                }
                            }
                            else
                            {
                                rptList.DataSource = null;
                                AspNetPagerList.RecordCount = 0;
                                rptList.DataBind();
                                LitPageTotal.Text = "1";
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageButtonsearch(object sender, ImageClickEventArgs e)
        {
            try
            {
                StringBuilder sbWhere = new StringBuilder();
                if (txtTitle.Value.Trim() != "")
                {
                    sbWhere.AppendFormat(" and Article.chvTitle like '%{0}%'", txtTitle.Value.Trim());
                }
                if (txtDatetime.Value.Trim() != "")
                {
                    sbWhere.AppendFormat(" and Article.dtmInTime>#{0}#", Convert.ToDateTime(txtDatetime.Value.Trim()));
                }
                if (txtKey.Value.Trim() != "")
                {
                    sbWhere.AppendFormat(" and Article.chvKeyWords like '%{0}%'", txtKey.Value.Trim());
                }
                State.strWhereSql = sbWhere.ToString();
                State.intPageIndex = 1;
                InitDataBind(State.intPageIndex, int.Parse(ddlShowCount.SelectedValue), ddlShowSort.SelectedValue);
            }
            catch (Exception ex)
            {
                Alert(ex.Message.Replace("\r\n", ""));
            }
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSort_Click(object sender, ImageClickEventArgs e)
        {
            if (SortHidden.Value != "")
            {
                Response.Redirect("ArticleSort.aspx?SortID=" + SortHidden.Value);
            }
            else
            {
                Alert("请选中要排序的文章！");
            }
        }

        /// <summary>
        /// 删除所选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteAll_Click(object sender, ImageClickEventArgs e)
        {
            SortHidden.Value = SortHidden.Value.Trim().Replace("'", "");
            BusinessLogic.BusinessRules.Sections.FindSectionReferRoot(State.intSTID, ref intSectionID);
            try
            {
                using (DBManager objDBManager = new DBManager())
                {
                    objDBManager.BeginTransaction();
                    try
                    {
                        SectionsFactory objSFactory = new SectionsFactory(objDBManager);
                        BusinessLogic.BusinessRules.Sections objSections = objSFactory.Creator(State.intSTID);
                        if (objSections != null)
                        {
                            if (!objSections.DataItem.IsintStateReferNull())
                            {
                                if (objSections.DataItem.intStateRefer == (int)Common.Utility.StateRefer.Yes)
                                {
                                    if (objSections.DataItem.IsintReferSectionIDNull())
                                    {
                                        Alert("该文章所属栏目设置了引用状态，但没有正确设置引用栏目，请检查");
                                        objSFactory.Dispose();
                                        objDBManager.Dispose();
                                        return;
                                    }
                                }
                            }
                        }
                        else
                        {
                            Alert("该文章所属栏目不存在，请检查");
                            objSFactory.Dispose();
                            objDBManager.Dispose();
                            return;
                        }

                        StringBuilder strsqlbuilder = new StringBuilder();
                        //关联表
                        objSections = objSFactory.CreatorBySql(" intSectionID=" + intSectionID);
                        int intDeleteResult = -1;

                        ArtSecRelationFactory objAsFactory = new ArtSecRelationFactory(objDBManager);
                        if (objSections != null)
                        {
                            string strAIDs = SortHidden.Value.Trim().Replace("'", "");
                            intDeleteResult = objAsFactory.DeleteWhere(" intSecArtID in (" + strAIDs + ")    and intSectionID=" + objSections.DataItem.intSectionID + " and intWSID=" + State.intWSID);
                            //文章表
                            ArticleFactory objFactory = new ArticleFactory(objDBManager);
                            strsqlbuilder.Append("update article set intRecycling=1  where intArticleID not in (select distinct intArticleID from ArtSecRelation   ) ");
                            BusinessLogic.BusinessRules.Article objArticle = objFactory.CreatorByFullSql(strsqlbuilder.ToString());
                        }

                        //栏目表
                        strsqlbuilder = new StringBuilder();
                        strsqlbuilder.Append("update Sections set intIssueState=" + (int)Utility.STIssueState.NoIssue + "  where  intSectionID=" + intSectionID + " or intReferSectionID=" + intSectionID);
                        objSections = objSFactory.CreatorByFullSql(strsqlbuilder.ToString());

                        BusinessLogic.BusinessRules.Sections objSectionsmain = objSFactory.CreatorBySql(" intParentId=0  and intWSID=" + State.intWSID);
                        if (objSectionsmain != null)
                        {
                            for (int i = 0; i < objSectionsmain.Count; i++)
                            {
                                objSectionsmain.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                                objSectionsmain.SelectRow(i + 1);
                            }
                            objSectionsmain.Update();
                        }

                        //提交
                        objDBManager.Commit();
                        if (intDeleteResult > 0)
                        {
                            AlertAndRedirect("删除成功", "../Section/List.aspx?Section=" + State.intSTID.ToString(), false);
                        }
                        else
                        {
                            AlertAndRedirect("删除未成功", "../Section/List.aspx?Section=" + State.intSTID.ToString(), false);
                        }
                        objSFactory.Dispose();
                        objAsFactory.Dispose();
                    }
                    catch (Exception ex)
                    {
                        Alert(ex.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 批量放入回收站
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRecovery_Click(object sender, ImageClickEventArgs e)
        {
        }

        /// <summary>
        /// 分页方法
        /// </summary>
        private void BindToPager()
        {
        }

        /// <summary>
        /// 分页  PageChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPagerArticle_PageChanged(object sender, EventArgs e)
        {
            State.intPageIndex = AspNetPagerList.CurrentPageIndex;
            InitDataBind(AspNetPagerList.CurrentPageIndex, int.Parse(ddlShowCount.SelectedValue), ddlShowSort.SelectedValue);
        }

        /// <summary>
        /// 选择呈现模版
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptList_Created(object sender, RepeaterItemEventArgs e)
        {
            bool bolArticleAutoSync = AppConfig.GetEnabledSyncPageBySave();
            if (bolArticleAutoSync)
            {
                Control controlBody = e.Item.FindControl("HasLookBody");
                e.Item.Controls.Clear();
                if (controlBody != null)
                {
                    e.Item.Controls.Add(controlBody);
                }
            }
            else
            {
                Control controlBody = e.Item.FindControl("NoLookBody");
                e.Item.Controls.Clear();
                if (controlBody != null)
                {
                    e.Item.Controls.Add(controlBody);
                }
            }
        }

        /// <summary>
        /// rptList_ItemCommand  编辑   放入回收站  删除
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                BusinessLogic.BusinessRules.Sections.FindSectionReferRoot(State.intSTID, ref intSectionID);
                using (DBManager objDBManager = new DBManager())
                {
                    int intSecArtID = Convert.ToInt32(e.CommandArgument);
                    objDBManager.BeginTransaction();
                    try
                    {
                        SectionsFactory objSFactory = new SectionsFactory(objDBManager);
                        BusinessLogic.BusinessRules.Sections objSections = objSFactory.Creator(State.intSTID);
                        if (objSections != null)
                        {
                            if (!objSections.DataItem.IsintStateReferNull())
                            {
                                if (objSections.DataItem.intStateRefer == (int)Common.Utility.StateRefer.Yes)
                                {
                                    if (objSections.DataItem.IsintReferSectionIDNull())
                                    {
                                        Alert("该文章所属栏目设置了引用状态，但没有正确设置引用栏目，请检查");
                                        objSFactory.Dispose();
                                        objDBManager.Dispose();
                                        return;
                                    }
                                }
                            }
                        }
                        else
                        {
                            Alert("该文章所属栏目不存在，请检查");
                            objSFactory.Dispose();
                            objDBManager.Dispose();
                            return;
                        }

                        StringBuilder strsqlbuilder = new StringBuilder();
                        //关联表
                        objSections = objSFactory.CreatorBySql(" intSectionID=" + intSectionID);
                        int intDeleteResult = -1;

                        ArtSecRelationFactory objAsFactory = new ArtSecRelationFactory(objDBManager);
                        if (objSections != null)
                        {
                            intDeleteResult = objAsFactory.DeleteWhere(" intSecArtID = " + intSecArtID + "  and intSectionID=" + objSections.DataItem.intSectionID + " and intWSID=" + State.intWSID);
                            //文章表
                            ArticleFactory objFactory = new ArticleFactory(objDBManager);
                            strsqlbuilder.Append("update article set intRecycling=1  where intArticleID not in (select distinct intArticleID from ArtSecRelation)");
                            BusinessLogic.BusinessRules.Article objArticle = objFactory.CreatorByFullSql(strsqlbuilder.ToString());
                        }

                        //栏目表
                        strsqlbuilder = new StringBuilder();
                        strsqlbuilder.Append("update Sections set intIssueState=" + (int)Utility.STIssueState.NoIssue + "  where  intSectionID=" + intSectionID + " or intReferSectionID=" + intSectionID);
                        objSections = objSFactory.CreatorByFullSql(strsqlbuilder.ToString());

                        BusinessLogic.BusinessRules.Sections objSectionsmain = objSFactory.CreatorBySql(" intParentId=0  and intWSID=" + State.intWSID);
                        if (objSectionsmain != null)
                        {
                            for (int i = 0; i < objSectionsmain.Count; i++)
                            {
                                objSectionsmain.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                                objSectionsmain.SelectRow(i + 1);
                            }
                            objSectionsmain.Update();
                        }

                        //提交
                        objDBManager.Commit();
                        if (intDeleteResult > 0)
                        {
                            AlertAndRedirect("删除成功", "../Section/List.aspx?Section=" + State.intSTID.ToString(), false);
                        }
                        else
                        {
                            AlertAndRedirect("删除未成功", "../Section/List.aspx?Section=" + State.intSTID.ToString(), false);
                        }
                        objSFactory.Dispose();
                        objAsFactory.Dispose();
                    }
                    catch (Exception ex)
                    {
                        Alert(ex.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();
                        return;
                    }
                }
            }
            //放入回收站
            if (e.CommandName == "Recycle")
            {
            }
        }

        /// <summary>
        /// 迁移文章
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSubmitChangeSection_Click(object sender, EventArgs e)
        {
            if (ParamAuth.IsNumber(ChangeSecHidden.Value.Trim()))
            {
                int intShangeSec = Convert.ToInt32(ChangeSecHidden.Value.Trim());
                try
                {
                    using (DBManager objDBManager = new DBManager())
                    {
                        objDBManager.BeginTransaction();
                        try
                        {
                            //关联表
                            ArtSecRelationFactory objAsFactory = new ArtSecRelationFactory(objDBManager);
                            BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objAsFactory.CreatorBySql(" intSecArtID in (" + SortHidden.Value.Trim() + ") and intWSID=" + State.intWSID);
                            if (objArtSecRelation != null)
                            {
                                //迁移到的不是本板块
                                if (intShangeSec != objArtSecRelation.DataItem.intSectionID)
                                {
                                    for (int i = 0; i < objArtSecRelation.Count; i++)
                                    {
                                        BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelationtmp = objAsFactory.CreatorBySql(" intArticleId=" + objArtSecRelation.DataItem.intArticleID + " and intSectionId=" + intShangeSec + " and intWSID=" + State.intWSID);
                                        if (objArtSecRelationtmp == null)
                                        {
                                            objArtSecRelation.DataItem.intSectionID = intShangeSec;
                                            objArtSecRelation.DataItem.blnIssue = (int)Utility.ArticleState.NoIssue;
                                            objArtSecRelation.DataItem.intState = (int)Utility.ArticleState.NoIssue;
                                        }
                                        else
                                        {
                                            objAsFactory.DeleteWhere(" intSecArtID =" + objArtSecRelation.DataItem.intSecArtID + "");
                                        }
                                        objArtSecRelation.SelectRow(i + 1);
                                    }
                                    objArtSecRelation.Update();
                                }
                                else
                                {
                                    Alert("不能移动到本板块");
                                    return;
                                }
                            }
                            //提交

                            SectionsFactory objSFactory = new SectionsFactory(objDBManager);
                            FindparentSection(State.intSTID);
                            FindparentSection(intShangeSec);

                            BusinessLogic.BusinessRules.Sections objSectionsmain = objSFactory.CreatorBySql(" intParentId=0  and intWSID=" + State.intWSID);
                            if (objSectionsmain != null)
                            {
                                for (int i = 0; i < objSectionsmain.Count; i++)
                                {
                                    objSectionsmain.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                                    objSectionsmain.SelectRow(i + 1);
                                }
                                objSectionsmain.Update();
                            }

                            objDBManager.Commit();
                            AlertAndRedirect("所选新闻已经迁移到指定板块", "../Section/List.aspx?Section=" + State.intSTID.ToString(), false);
                            objSFactory.Dispose();
                            objAsFactory.Dispose();
                        }
                        catch (Exception ex)
                        {
                            Alert(ex.ToString().Replace("\r\n", " "));
                            objDBManager.Rollback();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Alert(ex.ToString().Replace("\r\n", " "));
                }
            }
            else
            {
                Alert("参数错误");
            }
        }

        /// <summary>
        /// 共享文章
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSubmitShareSection_Click(object sender, EventArgs e)
        {
            if (ParamAuth.IsNumber(ChangeSecHidden.Value.Trim()))
            {
                int intShangeSec = Convert.ToInt32(ChangeSecHidden.Value.Trim());
                try
                {
                    using (DBManager objDBManager = new DBManager())
                    {
                        objDBManager.BeginTransaction();
                        try
                        {
                            //关联表
                            ArtSecRelationFactory objAsFactory = new ArtSecRelationFactory(objDBManager);
                            BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objAsFactory.CreatorBySql(" intSecArtID in (" + SortHidden.Value.Trim() + ") and intWSID=" + State.intWSID);
                            if (objArtSecRelation != null)
                            {
                                //共享到的不是本板块
                                if (intShangeSec != objArtSecRelation.DataItem.intSectionID)
                                {
                                    for (int i = 0; i < objArtSecRelation.Count; i++)
                                    {
                                        BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelationtmp = objAsFactory.CreatorBySql(" intArticleId=" + objArtSecRelation.DataItem.intArticleID + " and intSectionId=" + intShangeSec + " and intWSID=" + State.intWSID);
                                        if (objArtSecRelationtmp == null)
                                        {
                                            //插入关联表
                                            BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelationnew = objAsFactory.NewInstance();
                                            objArtSecRelationnew.DataItem.intSectionID = intShangeSec;
                                            objArtSecRelationnew.DataItem.intArticleID = objArtSecRelation.DataItem.intArticleID;
                                            //获取默认阅读数
                                            string strdefaultreadcount = System.Configuration.ConfigurationSettings.AppSettings["DefaultReadCount"] ?? "0";
                                            objArtSecRelationnew.DataItem.intAccessCount = ParamAuth.IsNumber(strdefaultreadcount) ? Convert.ToInt32(strdefaultreadcount) : 0;
                                            objArtSecRelationnew.DataItem.intSort = 0;
                                            objArtSecRelationnew.DataItem.dtmIssueTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo));
                                            objArtSecRelationnew.DataItem.blnIssue = (int)Utility.ArticleState.NoIssue;
                                            objArtSecRelationnew.DataItem.intState = (int)Utility.ArticleState.NoIssue;
                                            objArtSecRelationnew.DataItem.blnRefers = (int)Utility.ArticleRefersState.NoRefers;
                                            objArtSecRelationnew.DataItem.intWSID = objArtSecRelation.DataItem.intWSID;
                                            //文章位置状态
                                            objArtSecRelationnew.DataItem.intVerify = objArtSecRelation.DataItem.intVerify;
                                            objArtSecRelationnew.DataItem.intGlobalRec = 0;
                                            objArtSecRelationnew.DataItem.intSectionRec = 0;
                                            objArtSecRelationnew.DataItem.intGlobalTop = 0;
                                            objArtSecRelationnew.DataItem.intSectionTop = 0;
                                            objArtSecRelationnew.DataItem.intScroll = 0;
                                            objArtSecRelationnew.Insert();
                                        }
                                        objArtSecRelation.SelectRow(i + 1);
                                    }
                                    objArtSecRelation.Update();
                                }
                                else
                                {
                                    Alert("不能共享到本板块");
                                    return;
                                }
                            }
                            //提交

                            SectionsFactory objSFactory = new SectionsFactory(objDBManager);
                            //BusinessLogic.BusinessRules.Sections objSections = objSFactory.Creator(State.intSTID);
                            //objSections.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                            //objSections.Update();

                            FindparentSection(intShangeSec);

                            BusinessLogic.BusinessRules.Sections objSectionsmain = objSFactory.CreatorBySql(" intParentId=0  and intWSID=" + State.intWSID);
                            if (objSectionsmain != null)
                            {
                                for (int i = 0; i < objSectionsmain.Count; i++)
                                {
                                    objSectionsmain.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                                    objSectionsmain.SelectRow(i + 1);
                                }
                                objSectionsmain.Update();
                            }

                            objDBManager.Commit();
                            AlertAndRedirect("所选新闻已经共享到指定板块", "../Section/List.aspx?Section=" + State.intSTID.ToString(), false);
                            objSFactory.Dispose();
                            objAsFactory.Dispose();
                        }
                        catch (Exception ex)
                        {
                            Alert(ex.ToString().Replace("\r\n", " "));
                            objDBManager.Rollback();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Alert(ex.ToString().Replace("\r\n", " "));
                }
            }
            else
            {
                Alert("参数错误");
            }
        }

        /// <summary>
        /// 复制文章
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSubmitCopySection_Click(object sender, EventArgs e)
        {
            if (ParamAuth.IsNumber(ChangeSecHidden.Value.Trim()))
            {
                int intShangeSec = Convert.ToInt32(ChangeSecHidden.Value.Trim());
                try
                {
                    using (DBManager objDBManager = new DBManager())
                    {
                        objDBManager.BeginTransaction();
                        try
                        {
                            //关联表
                            ArtSecRelationFactory objAsFactory = new ArtSecRelationFactory(objDBManager);
                            BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objAsFactory.CreatorBySql(" intSecArtID in (" + SortHidden.Value.Trim() + ") and intWSID=" + State.intWSID);
                            if (objArtSecRelation != null)
                            {
                                //复制到的不是本板块
                                if (intShangeSec != objArtSecRelation.DataItem.intSectionID)
                                {
                                    for (int i = 0; i < objArtSecRelation.Count; i++)
                                    {
                                        BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelationtmp = objAsFactory.CreatorBySql(" intArticleId=" + objArtSecRelation.DataItem.intArticleID + " and intSectionId=" + intShangeSec + " and intWSID=" + State.intWSID);
                                        if (objArtSecRelationtmp == null)
                                        {
                                            ArticleFactory objFactory = new ArticleFactory(objDBManager);
                                            BusinessLogic.BusinessRules.Article objArticleSource = objFactory.Creator(objArtSecRelation.DataItem.intArticleID);
                                            BusinessLogic.BusinessRules.Article objArticle = objFactory.NewInstance();
                                            //插入文章表
                                            objArticle.DataItem.chvKeyWords = objArticleSource.DataItem.chvKeyWords;
                                            objArticle.DataItem.chvSummary = objArticleSource.DataItem.chvSummary;
                                            objArticle.DataItem.chvMainMiniPic = objArticleSource.DataItem.chvMainMiniPic;
                                            objArticle.DataItem.chvMainPic = objArticleSource.DataItem.chvMainPic;
                                            objArticle.DataItem.txtContent = objArticleSource.DataItem.txtContent;
                                            objArticle.DataItem.dtmInTime = DateTime.Now;
                                            objArticle.DataItem.chvTitle = objArticleSource.DataItem.chvTitle;
                                            objArticle.DataItem.chvSubTitle = objArticleSource.DataItem.chvSubTitle;
                                            objArticle.DataItem.chvArticleInMan = objArticleSource.DataItem.chvArticleInMan;
                                            objArticle.DataItem.chvDescription = objArticleSource.DataItem.chvDescription;
                                            objArticle.DataItem.chvRedirectUrl = objArticleSource.DataItem.chvRedirectUrl;
                                            objArticle.DataItem.chvVedioPath = objArticleSource.DataItem.chvVedioPath;
                                            objArticle.DataItem.chvFilePath = objArticleSource.DataItem.chvFilePath;
                                            objArticle.DataItem.chvArticleSource = objArticleSource.DataItem.chvArticleSource;
                                            objArticle.DataItem.chvAuthor = objArticleSource.DataItem.chvAuthor;
                                            objArticle.DataItem.txtSeoTitle = objArticleSource.DataItem.txtSeoTitle;
                                            objArticle.DataItem.txtSeoKeywords = objArticleSource.DataItem.txtSeoKeywords;
                                            objArticle.DataItem.txtSeodescription = objArticleSource.DataItem.txtSeodescription;
                                            objArticle.DataItem.dtmIssueTime = Convert.ToDateTime(DateTime.Now.ToString("yy/MM/dd HH:mm"));
                                            objArticle.DataItem.intRecycling = (int)Utility.ArticleState.NoRecycling;
                                            objArticle.DataItem.intRecycling = objArticleSource.DataItem.intWSID;
                                            objArticle.DataItem.intWSID = State.intWSID;
                                            objArticle.Insert();
                                            //插入关联表
                                            BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelationnew = objAsFactory.NewInstance();
                                            objArtSecRelationnew.DataItem.intSectionID = intShangeSec;
                                            objArtSecRelationnew.DataItem.intArticleID = objArticle.DataItem.intArticleID;
                                            //获取默认阅读数
                                            string strdefaultreadcount = System.Configuration.ConfigurationSettings.AppSettings["DefaultReadCount"] ?? "0";
                                            objArtSecRelationnew.DataItem.intAccessCount = ParamAuth.IsNumber(strdefaultreadcount) ? Convert.ToInt32(strdefaultreadcount) : 0;
                                            objArtSecRelationnew.DataItem.intSort = 0;
                                            objArtSecRelationnew.DataItem.dtmIssueTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo));
                                            objArtSecRelationnew.DataItem.blnIssue = (int)Utility.ArticleState.NoIssue;
                                            objArtSecRelationnew.DataItem.blnRefers = (int)Utility.ArticleRefersState.NoRefers;
                                            objArtSecRelationnew.DataItem.intState = (int)Utility.ArticleState.NoIssue;
                                            objArtSecRelationnew.DataItem.intType = (int)Utility.ArticleType.Article;
                                            objArtSecRelationnew.DataItem.intWSID = State.intWSID;
                                            //文章位置状态
                                            objArtSecRelationnew.DataItem.intVerify = objArtSecRelation.DataItem.intVerify;
                                            objArtSecRelationnew.DataItem.intGlobalRec = 0;
                                            objArtSecRelationnew.DataItem.intSectionRec = 0;
                                            objArtSecRelationnew.DataItem.intGlobalTop = 0;
                                            objArtSecRelationnew.DataItem.intSectionTop = 0;
                                            objArtSecRelationnew.DataItem.intScroll = 0;
                                            objArtSecRelationnew.Insert();
                                        }
                                        objArtSecRelation.SelectRow(i + 1);
                                    }
                                    objArtSecRelation.Update();
                                }
                                else
                                {
                                    Alert("不能复制到本板块");
                                    return;
                                }
                            }
                            //提交

                            SectionsFactory objSFactory = new SectionsFactory(objDBManager);
                            //BusinessLogic.BusinessRules.Sections objSections = objSFactory.Creator(State.intSTID);
                            //objSections.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;

                            //objSections.Update();

                            FindparentSection(intShangeSec);

                            BusinessLogic.BusinessRules.Sections objSectionsmain = objSFactory.CreatorBySql(" intParentId=0  and intWSID=" + State.intWSID);
                            if (objSectionsmain != null)
                            {
                                for (int i = 0; i < objSectionsmain.Count; i++)
                                {
                                    objSectionsmain.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                                    objSectionsmain.SelectRow(i + 1);
                                }
                                objSectionsmain.Update();
                            }

                            objDBManager.Commit();
                            AlertAndRedirect("所选新闻已经复制到到指定板块", "../Section/List.aspx?Section=" + State.intSTID.ToString(), false);
                            objSFactory.Dispose();
                            objAsFactory.Dispose();
                        }
                        catch (Exception ex)
                        {
                            Alert(ex.ToString().Replace("\r\n", " "));
                            objDBManager.Rollback();
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Alert(ex.ToString().Replace("\r\n", " "));
                }
            }
            else
            {
                Alert("参数错误");
            }
        }

        //显示条数
        protected void ddlShowCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitDataBind(State.intPageIndex, int.Parse(ddlShowCount.SelectedValue), ddlShowSort.SelectedValue);
        }

        //排序方式
        protected void ddlSortshowCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitDataBind(State.intPageIndex, int.Parse(ddlShowCount.SelectedValue), ddlShowSort.SelectedValue);
        }

        private void FindparentSection(int intSectionId)
        {
            using (SectionsFactory objSFactory = new SectionsFactory())
            {
                BusinessLogic.BusinessRules.Sections objSections = objSFactory.Creator(intSectionId);
                if (objSections != null)
                {
                    objSections.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                    objSections.Update();
                    objSections = objSFactory.CreatorBySql(" intParentId=" + objSections.DataItem.intParentID);
                    if (objSections != null)
                    {
                        objSections.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                        objSections.Update();
                        FindparentSection(objSections.DataItem.intParentID);
                    }
                }
            }
        }
    }
}